/*会议天日历控件*/
/**
 * 设置日历导航日期
 */
(function ($) {
    var selectedIndex;
    var curPage;
    var totlePage;
    var curData;
    $.fn.extend({
        // 导航日历日期
        setNavigateDay: function (options) {
            var settings = $.extend({
                startTime: null,
                endTime: null,
                week: null
            }, options);
            return this.each(function () {
                _this = $(this);
                var date1 = formatStringToDate(settings.startTime, 0);
                var week = getWeek(settings.week);
                _this.find("#selecteDatetimeTable").find("tr").find("td").eq(2).text(formatDateToStringCN(date1) + week);
                _this.find("#btnPrev").click(function () {
                    var date1 = formatStringToDate(settings.startTime, -1);
                    var date2 = formatStringToDate(settings.endTime, -1);
                    var curDate = formatStringToDate(formatDateToStringEN(new Date()) + " 00:00:00", 0);
                    if (date1.getTime() >= curDate.getTime()) {
                        settings.startTime = formatDateToStringEN(date1) + " 00:00:00";
                        settings.endTime = formatDateToStringEN(date2) + " 23:59:59";
                        if (settings.week == 0) {
                            settings.week = 7;
                        }
                        settings.week = settings.week - 1;
                        var week = getWeek(settings.week);
                        _this.find("#selecteDatetimeTable").find("tr").find("td").eq(2).text(formatDateToStringCN(date1) + week);
                        _this.trigger("selectPrevDate", [settings.startTime, settings.endTime]);
                    }
                });
                _this.find("#btnNext").click(function () {
                    var date1 = formatStringToDate(settings.startTime, 1);
                    var date2 = formatStringToDate(settings.endTime, 1);
                    settings.startTime = formatDateToStringEN(date1) + " 00:00:00";
                    settings.endTime = formatDateToStringEN(date2) + " 23:59:59";
                    settings.week = settings.week + 1;
                    if (settings.week == 7) {
                        settings.week = 0;
                    }
                    var week = getWeek(settings.week);
                    _this.find("#selecteDatetimeTable").find("tr").find("td").eq(2).text(formatDateToStringCN(date1) + week);
                    _this.trigger("selectNextDate", [settings.startTime, settings.endTime]);
                });

                function formatDateToStringEN(dt) {
                    var strYear = dt.getFullYear();
                    var strMonth = (dt.getMonth() + 1);
                    if (strMonth < 10) {
                        strMonth = "0" + strMonth;
                    }
                    var strDay = dt.getDate();
                    if (strDay < 10) {
                        strDay = "0" + strDay;
                    }
                    return strYear + "-" + strMonth + "-" + strDay;
                }

                function formatDateToStringCN(dt) {
                    var strYear = dt.getFullYear();
                    var strMonth = (dt.getMonth() + 1);
                    if (strMonth < 10) {
                        strMonth = "0" + strMonth;
                    }
                    var strDay = dt.getDate();
                    if (strDay < 10) {
                        strDay = "0" + strDay;
                    }
                    return strYear + "年" + strMonth + "月" + strDay + "日";
                }

                function formatStringToDate(dt, interval) {
                    var date = new Date(dt.replace(/-/ig, '/'));
                    date.setDate(date.getDate() + interval);
                    return date;
                }

                function getWeek(dt) {
                    var week = "";
                    switch (dt) {
                        case 0:
                            week = "星期日";
                            break;
                        case 1:
                            week = "星期一";
                            break;
                        case 2:
                            week = "星期二";
                            break;
                        case 3:
                            week = "星期三";
                            break;
                        case 4:
                            week = "星期四";
                            break;
                        case 5:
                            week = "星期五";
                            break;
                        case 6:
                            week = "星期六";
                            break;
                        default:
                            break;
                    }
                    return week;
                }
            });
        },
        // 初始化会议室
        initBoardroom: function (options) {
            var settings = $.extend({
                id: null,
                data: null,
                status: null
            }, options);
            return this.each(function () {
                _this = $(this);
                selectedIndex = 0;
                curPage = 1;
                totlePage = 0;
                curData = settings.data;
                if (settings.data != null && settings.data.length > 0) {
                    if (settings.data.length % 7 == 0) {
                        totlePage = parseInt(settings.data.length / 7);
                    } else {
                        totlePage = parseInt(settings.data.length / 7) + 1;
                    }
                    if (settings.id != null) {
                        for (var i = 0; i < settings.data.length; i++) {
                            if (settings.data[i].id == settings.id) {
                                selectedIndex = i;
                                if ((i + 1) % 7 == 0) {
                                    curPage = parseInt((i + 1) / 7);

                                } else {
                                    curPage = parseInt((i + 1) / 7) + 1;
                                }
                                break;
                            }
                        }
                    }
                    createBoardroom(curData);
                    if (selectedIndex > 0) {
                        var index = (selectedIndex - (curPage - 1) * 7) + 1;
                        reomveSelected();
                        _this.trigger("boardroomselected", [settings.data[selectedIndex]]);
                        _this.find("#meetingCalendar").find("tr").eq(index).find("td").eq(0).addClass("calendar-td-selected-color");
                    } else {
                        _this.trigger("boardroomselected", [settings.data[0]]);
                    }
                } else {
                    cleanBoardroom();
                    reomveSelected();
                    _this.trigger("boardroomselected", [null]);
                    _this.trigger("pagechange", [""]);
                }
                if (settings.status == 0) {
                    _this.find("#meetingCalendar").find("tr").eq(1).find("td").eq(0).click(function () {
                        if ($(this).text() != null && $(this).text() != "") {
                            reomveSelected();
                            $(this).addClass("calendar-td-selected-color");
                            data = getBoardroom($(this).attr("id"), curData);
                            _this.trigger("boardroomselected", [data]);
                        }
                    });
                    _this.find("#meetingCalendar").find("tr").eq(2).find("td").eq(0).click(function () {
                        if ($(this).text() != null && $(this).text() != "") {
                            reomveSelected();
                            $(this).addClass("calendar-td-selected-color");
                            data = getBoardroom($(this).attr("id"), curData);
                            _this.trigger("boardroomselected", [data]);
                        }
                    });
                    _this.find("#meetingCalendar").find("tr").eq(3).find("td").eq(0).click(function () {
                        if ($(this).text() != null && $(this).text() != "") {
                            reomveSelected();
                            $(this).addClass("calendar-td-selected-color");
                            data = getBoardroom($(this).attr("id"), curData);
                            _this.trigger("boardroomselected", [data]);
                        }
                    });
                    _this.find("#meetingCalendar").find("tr").eq(4).find("td").eq(0).click(function () {
                        if ($(this).text() != null && $(this).text() != "") {
                            reomveSelected();
                            $(this).addClass("calendar-td-selected-color");
                            data = getBoardroom($(this).attr("id"), curData);
                            _this.trigger("boardroomselected", [data]);
                        }
                    });
                    _this.find("#meetingCalendar").find("tr").eq(5).find("td").eq(0).click(function () {
                        if ($(this).text() != null && $(this).text() != "") {
                            reomveSelected();
                            $(this).addClass("calendar-td-selected-color");
                            data = getBoardroom($(this).attr("id"), curData);
                            _this.trigger("boardroomselected", [data]);
                        }
                    });
                    _this.find("#meetingCalendar").find("tr").eq(6).find("td").eq(0).click(function () {
                        if ($(this).text() != null && $(this).text() != "") {
                            reomveSelected();
                            $(this).addClass("calendar-td-selected-color");
                            data = getBoardroom($(this).attr("id"), curData);
                            _this.trigger("boardroomselected", [data]);
                        }
                    });
                    _this.find("#meetingCalendar").find("tr").eq(7).find("td").eq(0).click(function () {
                        if ($(this).text() != null && $(this).text() != "") {
                            reomveSelected();
                            $(this).addClass("calendar-td-selected-color");
                            data = getBoardroom($(this).attr("id"), curData);
                            _this.trigger("boardroomselected", [data]);
                        }
                    });
                    _this.find("#btnLast").click(function () {
                        curPage--;
                        if (curPage < 1) {
                            curPage = 1;
                        }
                        createBoardroom(curData);
                        id = _this.find("#meetingCalendar").find("tr").eq(1).find("td").eq(0).attr("id");
                        data = getBoardroom(id, curData);
                        _this.trigger("boardroomselected", [data]);
                    });
                    _this.find("#btnAfter").click(function () {
                        curPage++;
                        if (curPage > totlePage) {
                            curPage = totlePage;
                        }
                        createBoardroom(curData);
                        id = _this.find("#meetingCalendar").find("tr").eq(1).find("td").eq(0).attr("id");
                        data = getBoardroom(id, curData);
                        _this.trigger("boardroomselected", [data]);
                    });
                }

                function createBoardroom(data) {
                    var value = "";
                    cleanBoardroom();
                    if (curPage == totlePage) {
                        var index = (curPage - 1) * 7;
                        var trIndex = 1;
                        for (var i = index; i < data.length; i++) {
                            _this.find("#meetingCalendar").find("tr").eq(trIndex).find("td").eq(0).attr("id", data[i].id);
                            _this.find("#meetingCalendar").find("tr").eq(trIndex).find("td").eq(0).text(data[i].boardroomName);
                            _this.find("#meetingCalendar").find("tr").eq(trIndex).find("td").eq(0).mouseenter(function () {
                                if ($(this).text() != null && $(this).text() != "") {
                                    $(this).addClass("calendar-td-hover");
                                }
                            });
                            _this.find("#meetingCalendar").find("tr").eq(trIndex).find("td").eq(0).mouseleave(function () {
                                $(this).removeClass("calendar-td-hover");
                            });
                            trIndex++;
                        }
                    } else {
                        var index = (curPage - 1) * 7;
                        var trIndex = 1;
                        for (var i = index; i < curPage * 7; i++) {
                            _this.find("#meetingCalendar").find("tr").eq(trIndex).find("td").eq(0).attr("id", data[i].id);
                            _this.find("#meetingCalendar").find("tr").eq(trIndex).find("td").eq(0).text(data[i].boardroomName);
                            _this.find("#meetingCalendar").find("tr").eq(trIndex).find("td").eq(0).mouseenter(function () {
                                if ($(this).text() != null && $(this).text() != "") {
                                    $(this).addClass("calendar-td-hover");
                                }
                            });
                            _this.find("#meetingCalendar").find("tr").eq(trIndex).find("td").eq(0).mouseleave(function () {
                                $(this).removeClass("calendar-td-hover");
                            });
                            trIndex++;
                        }
                    }
                    reomveSelected();
                    _this.find("#meetingCalendar").find("tr").eq(1).find("td").eq(0).addClass("calendar-td-selected-color");
                    for (var i = 0; i < _this.find("#meetingCalendar").find("tr").length; i++) {
                        if (i != 0) {
                            id = _this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(0).attr("id");
                            if (id != null && id != "") {
                                value += id + ",";
                            }
                        }
                    }
                    _this.trigger("pagechange", [value]);
                }

                function getBoardroom(id, data) {
                    var result = null;
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].id == id) {
                            result = data[i];
                            break;
                        }
                    }
                    return result;
                }

                function cleanBoardroom() {
                    for (var i = 0; i < _this.find("#meetingCalendar").find("tr").length; i++) {
                        if (i != 0) {
                            _this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(0).text("");
                            _this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(0).attr("id", "");
                        }
                    }
                }

                function reomveSelected() {
                    for (var i = 0; i < _this.find("#meetingCalendar").find("tr").length; i++) {
                        if (i != 0) {
                            _this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(0).removeClass("calendar-td-selected-color");
                        }
                    }
                }
            });
        },
        // 绘制会议日历
        drewDayCalendar: function (options) {
            var settings = $.extend({
                data: null,
                curDateTime: null
            }, options);
            return this.each(function () {
                _this = $(this);
                var data = settings.data;
                _this.find("#meetingCalendar").find("td").removeClass("calendar-booking");
                _this.find("#meetingCalendar").find("td").removeClass("calendar-finish");
                _this.find("#meetingCalendar").find("td").removeClass("calendar-book");
                _this.find("#meetingCalendar").find("td").removeClass("calendar-important-book");
                if (data != null) {
                    for (var i = 0; i < data.length; i++) {
                        for (var j = 0; j < data[i].length; j++) {
                            var starttime = new Date(data[i][j].startTime.replace(/-/ig, '/'));
                            var endtime = new Date(data[i][j].endTime.replace(/-/ig, '/'));
                            var td = (starttime.getHours() - 7) * 4 + starttime.getMinutes() / 15;
                            switch (data[i][j].status) {
                                case "0":
                                    for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                        _this.find("#meetingCalendar").find("tr").eq(i + 1).find("td").eq(td + a).addClass("calendar-booking");
                                    }
                                    break;
                                case "1":
                                    for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                        _this.find("#meetingCalendar").find("tr").eq(i + 1).find("td").eq(td + a).addClass("calendar-booking");
                                    }
                                    break;
                                case "2":
                                    for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                        _this.find("#meetingCalendar").find("tr").eq(i + 1).find("td").eq(td + a).addClass("calendar-booking");
                                    }
                                    break;
                                case "3":
                                    for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                        _this.find("#meetingCalendar").find("tr").eq(i + 1).find("td").eq(td + a).addClass("calendar-booking");
                                    }
                                    break;
                                case "4":
                                    for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                        _this.find("#meetingCalendar").find("tr").eq(i + 1).find("td").eq(td + a).addClass("calendar-booking");
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }

                // 根据当前时间绘制日历不可选区域
                if (settings.curDateTime != null) {
                    if (settings.curDateTime.split(" ")[0] == formatDateToStringEN(new Date())) {
                        var startDateTime = formatDateToStringEN(new Date()) + " 07:00:00";
                        var endDateTime = getEndDateTime(new Date());
                        for (var i = 1; i < _this.find("#meetingCalendar").find("tr").length; i++) {
                            var starttime = new Date(startDateTime.replace(/-/ig, '/'));
                            var endtime = new Date(endDateTime.replace(/-/ig, '/'));
                            var td = (starttime.getHours() - 7) * 4 + starttime.getMinutes() / 15;
                            for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                _this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(td + a).addClass("calendar-finish");
                            }
                        }
                    }
                }

                function formatDateToStringEN(dt) {
                    var strYear = dt.getFullYear();
                    var strMonth = (dt.getMonth() + 1);
                    if (strMonth < 10) {
                        strMonth = "0" + strMonth;
                    }
                    var strDay = dt.getDate();
                    if (strDay < 10) {
                        strDay = "0" + strDay;
                    }
                    return strYear + "-" + strMonth + "-" + strDay;
                }

                function getEndDateTime(dt) {
                    var year = dt.getFullYear();
                    var month = (dt.getMonth() + 1);
                    if (month < 10) {
                        month = "0" + month;
                    }
                    var day = dt.getDate();
                    if (day < 10) {
                        day = "0" + day;
                    }
                    var time = null;
                    if (dt.getMinutes() > 0 && dt.getMinutes() <= 15) {
                        hour = dt.getHours();
                        if (hour < 10) {
                            hour = "0" + hour;
                        }
                        time = hour + ":15:00";
                    } else if (dt.getMinutes() > 15 && dt.getMinutes() <= 30) {
                        hour = dt.getHours();
                        if (hour < 10) {
                            hour = "0" + hour;
                        }
                        time = hour + ":30:00";
                    } else if (dt.getMinutes() > 30 && dt.getMinutes() <= 45) {
                        hour = dt.getHours();
                        if (hour < 10) {
                            hour = "0" + hour;
                        }
                        time = hour + ":45:00";
                    } else {
                        hour = dt.getHours();
                        hour = hour + 1;
                        if (hour < 10) {
                            hour = "0" + hour;
                        }
                        time = hour + ":00:00";
                    }
                    return year + "-" + month + "-" + day + " " + time;
                }
            });
        },
        // 绘制预定会议日历
        drewBookingDayCalendar: function (options) {
            var settings = $.extend({
                id: null,
                startTime: null,
                endTime: null,
                interval: 0,
                flag: "0"
            }, options);
            return this.each(function () {
                var flag = false;
                _this = $(this);
                _this.find("#meetingCalendar").find("td").removeClass("calendar-book");
                _this.find("#meetingCalendar").find("td").removeClass("calendar-important-book");
                stIndex = settings.startTime.indexOf(":");
                etIndex = settings.endTime.indexOf(":");
                if (stIndex != 0 && stIndex != settings.startTime.length - 1 && etIndex != 0 && etIndex != settings.endTime.length - 1 &&
                    settings.id != null) {
                    var startHour = parseInt(settings.startTime.split(":")[0]);
                    var startMinute = parseInt(settings.startTime.split(":")[1]);
                    var endHour = parseInt(settings.endTime.split(":")[0]);
                    var endMinute = parseInt(settings.endTime.split(":")[1]);
                    var td = (startHour - 7) * 4 + startMinute / 15;
                    var length = (endHour * 4 + endMinute / 15) - (startHour * 4 + startMinute / 15);
                    if (length > 0) {
                        for (var i = 1; i < _this.find("#meetingCalendar").find("tr").length; i++) {
                            var id = _this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(0).attr("id");
                            if (settings.id == id) {
                                if (settings.flag == "1") {
                                    var td1 = td - parseInt(settings.interval / 15);
                                    var length1 = length + parseInt(settings.interval / 15) * 2;
                                    for (var j = 1; j <= length1; j++) {
                                        if (_this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(td1 + j).hasClass("calendar-finish")) {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (!flag) {
                                        for (var j = 1; j <= length; j++) {
                                            _this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(td + j).addClass("calendar-important-book");
                                        }
                                        var hour = parseInt(((endHour * 60 + endMinute) - (startHour * 60 + startMinute)) / 60);
                                        var minute = ((endHour * 60 + endMinute) - (startHour * 60 + startMinute)) % 60;
                                        var value = hour + "小时" + minute + "分钟";
                                        _this.trigger("conferencetimeload", [value]);
                                    } else {
                                        _this.trigger("conferencetimeload", [""]);
                                    }
                                } else {
                                    var td1 = td - parseInt(settings.interval / 15);
                                    var length1 = length + parseInt(settings.interval / 15) * 2;
                                    for (var j = 1; j <= length1; j++) {
                                        if (_this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(td1 + j).hasClass("calendar-booking") ||
                                            _this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(td1 + j).hasClass("calendar-finish")) {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (!flag) {
                                        for (var j = 1; j <= length; j++) {
                                            _this.find("#meetingCalendar").find("tr").eq(i).find("td").eq(td + j).addClass("calendar-book");
                                        }
                                        var hour = parseInt(((endHour * 60 + endMinute) - (startHour * 60 + startMinute)) / 60);
                                        var minute = ((endHour * 60 + endMinute) - (startHour * 60 + startMinute)) % 60;
                                        var value = hour + "小时" + minute + "分钟";
                                        _this.trigger("conferencetimeload", [value]);
                                    } else {
                                        _this.trigger("conferencetimeload", [""]);
                                    }
                                }
                                return;
                            }
                        }
                    }
                }
                _this.trigger("conferencetimeload", [""]);
            });
        }
    });
})(jQuery);